//
//  WeekPlanLayout.m
//  PlanDemo
//
//  Created by 在成长开发 on 2025/9/8.
//

#import "WeekPlanLayout.h"

@interface WeekPlanLayout ()

@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *cached;

@property (nonatomic, assign) CGFloat contentWidth;
@property (nonatomic, assign) CGFloat contentHeight;

@end

@implementation WeekPlanLayout

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)prepareLayout {
    [super prepareLayout];
    
    if (self.cached.count) { return; }
    
    self.cached = [NSMutableArray array];
    
    CGFloat totalWidth = 0;
    CGFloat totalHeight = 0;
    
    NSInteger sectionCount = [self.collectionView numberOfSections];
    for (NSInteger section = 0; section < sectionCount; section ++) {
        // 获取分组基本配置信息
        UIEdgeInsets sectionInset = UIEdgeInsetsZero;
        
        if ([self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
            sectionInset = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
            
        } else if (UIEdgeInsetsEqualToEdgeInsets(self.sectionInset, UIEdgeInsetsZero) == NO) {
            sectionInset = self.sectionInset;
        }
        
        CGFloat lineSpace = 0;
        if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumLineSpacingForSectionAtIndex:)]) {
            lineSpace = [self.delegate collectionView:self.collectionView layout:self minimumLineSpacingForSectionAtIndex:section];
        } else {
            lineSpace = self.minimumLineSpacing;
        }
        
        CGFloat itemSpace = 0;
        if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
            itemSpace = [self.delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:section];
        } else {
            itemSpace = self.minimumInteritemSpacing;
        }
        
        NSInteger count = [self.collectionView numberOfItemsInSection:section];
        for (NSInteger i = 0; i < count; i++) {
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:section];
            
            CGSize size = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
            
            UICollectionViewLayoutAttributes * attr =
            [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
            
            CGFloat fromRow = [self.delegate collectionView:self.collectionView layout:self atRowForItemAtIndexPath:indexPath];
            CGFloat fromCol = [self.delegate collectionView:self.collectionView layout:self atColForItemAtIndexPath:indexPath];
            
            CGFloat top = sectionInset.top + (fromRow == 0 ? 0 : ((kSingleRowWidth + kLineSpace) * fromRow/* - kLineSpace*/));
            CGFloat left = sectionInset.left + (fromCol == 0 ? 0 : ((kSingleColWidth + kItemSpace) * fromCol/* - kItemSpace*/));
            
            attr.frame = CGRectMake(left, top, size.width, size.height);
            
            [self.cached addObject:attr];
            
            totalWidth = MAX(totalWidth, left + size.width + sectionInset.right);
            totalHeight = MAX(totalHeight, top + size.height + sectionInset.bottom);
        }
    }
    self.contentWidth = totalWidth;
    self.contentHeight = totalHeight;
    NSLog(@"内容宽度： %f, 内容高度：%f", self.contentWidth, self.contentHeight);
}

- (CGSize)collectionViewContentSize {
    return CGSizeMake(self.contentWidth, self.contentHeight);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.cached[indexPath.item];
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    if (self.cached.count == 0 || self.collectionView.numberOfSections == 0) {
        return [super layoutAttributesForElementsInRect:rect];
    }
    return self.cached;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return !CGSizeEqualToSize(newBounds.size, self.collectionView.bounds.size);
}

@end
